﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SupermarketManager.Core;
using SupermarketManager.Model;

namespace SupermaketManager.DataAccess
{
    class KhachhangAccess
    {
        dbConnection db = new dbConnection();
        public DataTable Laytoanbokh()
        {
            return db.executeSelectProcedureQueryNonParameter("select_khachhang");
        }
        //---- Tìm kiếm theo mã kh
        public DataView timkiemMakh(string makh)
        {
            DataView dataView = Laytoanbokh().DefaultView;
            dataView.RowFilter = string.Format("maKH like '%{0}%'", makh);
            return dataView;
        }
        //---- Tìm kiếm theo tên kh 
        public DataView timkiemtenkh(string Tenkh)
        {
            DataView dataView = Laytoanbokh().DefaultView;
            dataView.RowFilter = string.Format("tenKH like '%{0}%'", Tenkh);
            return dataView;
        }
        public DataTable timkhtheoma(string makh)
        {
            SqlParameter[] listParam = new SqlParameter[1];
            SqlParameter ma = new SqlParameter("@maKH", SqlDbType.Char, 10);
            ma.Value = makh;
            listParam[0] = ma;
            return db.executeSelectProcedureQuery("khachhang_theoma", listParam);
        }
        //---- Thêm kh
        public int themkh(KhachHang kh)
        {

            if (timkhtheoma(kh.MaKH).Rows.Count > 0) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[7];
                SqlParameter makh = new SqlParameter("@maKH", SqlDbType.VarChar, 10);
                makh.Value = kh.MaKH;
                listParam[0] = makh;
                listParam[1] = new SqlParameter("@hodemKH", kh.HodemKH);
                listParam[2] = new SqlParameter("@tenKH", kh.TenKH);
                listParam[3] = new SqlParameter("@gioitinh", kh.GioiTinh);
                listParam[4] = new SqlParameter("@diaChi", kh.DiaChi);
                listParam[5] = new SqlParameter("@soDT", kh.SoDT);
                listParam[6] = new SqlParameter("@email", kh.Email);
                db.executeInsertProcedureQuery("insert_khachhang", listParam);
                return 1;
            }
        }
        //---- Sửa kh 
        public int suakh(KhachHang kh)
        {
            if ((timkhtheoma(kh.MaKH) == null))
                return 0;
            else
            {

                SqlParameter[] listParam = new SqlParameter[7];
                SqlParameter makh = new SqlParameter("@maKH", SqlDbType.VarChar, 10);
                makh.Value = kh.MaKH;
                listParam[0] = makh;
                listParam[1] = new SqlParameter("@hodemKH", kh.HodemKH);
                listParam[2] = new SqlParameter("@tenKH", kh.TenKH);
                listParam[3] = new SqlParameter("@gioitinh", kh.GioiTinh);
                listParam[4] = new SqlParameter("@diaChi", kh.DiaChi);
                listParam[5] = new SqlParameter("@soDT", kh.SoDT);
                listParam[6] = new SqlParameter("@email", kh.Email);
                db.executeInsertProcedureQuery("update_khachhang", listParam);
                return 1;

            }
        }
        // xóa kh
        public int xoakh(KhachHang kh)
        {
            if ((timkhtheoma(kh.MaKH) == null))
                return 0;
            else
            {

                SqlParameter[] listParam = new SqlParameter[7];
                SqlParameter makh = new SqlParameter("@maKH", SqlDbType.VarChar, 10);
                makh.Value = kh.MaKH;
                listParam[0] = makh;
                listParam[1] = new SqlParameter("@hodemKH", kh.HodemKH);
                listParam[2] = new SqlParameter("@tenKH", kh.TenKH);
                listParam[3] = new SqlParameter("@gioitinh", kh.GioiTinh);
                listParam[4] = new SqlParameter("@diaChi", kh.DiaChi);
                listParam[5] = new SqlParameter("@soDT", kh.SoDT);
                listParam[6] = new SqlParameter("@email", kh.Email);
                db.executeInsertProcedureQuery("delete_khachhang", listParam);
                return 1;

            }

        }
    }
}
